tibco.admin.Main.loadResource("tibco/admin/deployment/sa/updateSA/jss/jss.xml", "tibco.admin", "jss43", "ljss");

/*
* Update Service Assembly Detail(General Information) logical code
* tibco/admin/deployment/sa/updateSA
* Modified by Amour GUO 04/29/2007
*/

/*
* Define updateSA Package if not exist.
*/
if(!jsx3.lang.Package.forName("tibco.admin.deployment.sa.updateSA")) {
    jsx3.lang.Package.definePackage("tibco.admin.deployment.sa.updateSA", function (udSA) {
    });
}

/**
 * define UpdateServiceAssembly class;
 * className: tibco.admin.deployment.sa.updateSA.UpdateServiceAssembly;
 */
jsx3.lang.Class.defineClass(
        'tibco.admin.deployment.sa.updateSA.UpdateServiceAssembly',
        jsx3.lang.Object,
        [tibco.admin.deployment.sa.ServiceAssemblyInterface],
        function(updateSA) {

            updateSA.prototype.block = null;
            //root block of this component.

            updateSA.prototype.serviceAssembly = "";
            //Name of Service Assembly
            updateSA.prototype.archivePath = "";
            //Archive File
            updateSA.prototype.contact = "";
            //Contact
            updateSA.prototype.assemblyEntityId = "";
            //Entity ID of Service Assembly
            updateSA.prototype.description = "";
            //Description
            updateSA.prototype.publishToUddi = "";
            //Prefer Local Providers?
            updateSA.prototype.nameSpace = "";
            //Namespace
            updateSA.prototype.state = "";
            //Status
            updateSA.prototype.packagedBy = "";
            //Packaged By
            updateSA.prototype.exposeEndpoints = "";
            //Expose Endpoint
            updateSA.prototype.updateSuccess = "";
            //Success flag returns from backend
            updateSA.prototype.envEntityID = "";
            //Environment Entity ID

            //Hash: Status display text, can be located by Status code returnd by the backend.
            updateSA.prototype.stateCode = {
                "UNKNOWN"     : tibco.admin.Main.getServer().getDynamicProperty("@@UNKNOWN_43"),
                "NOTDEPLOYED" : tibco.admin.Main.getServer().getDynamicProperty("@@NOTDEPLOYED_43"),
                "DEPLOYED"    : tibco.admin.Main.getServer().getDynamicProperty("@@DEPLOYED_43"),
                "RUNNING"     : tibco.admin.Main.getServer().getDynamicProperty("@@RUNNING_43"),
                "DEPLOYED_WITH_ERROR" : tibco.admin.Main.getServer().getDynamicProperty("@@DEPLOYED_WITH_ERROR_43"),
                "RUNNING_WITH_ERROR"  : tibco.admin.Main.getServer().getDynamicProperty("@@RUNNING_WITH_ERROR_43")
            }

            updateSA.prototype.getButtonText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@lblHeader_43");
            }

            updateSA.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            updateSA.getInstance = function() {
                if(!this.instance) {
                    this.instance = new tibco.admin.deployment.sa.updateSA.UpdateServiceAssembly();
                }
                return this.instance;
            }

            updateSA.prototype.onAfterComponentUnloaded = function() {
            }

            updateSA.prototype.loadComponent = function(parent) {
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/deployment/sa/updateSA/ui/saDetails.xml", me, me.componentLoaded);
            }

            /**
             * populates the configure description panel
             * @param message {tibco.admin.util.Msg}
             */
            updateSA.prototype.setDetails = function(message) {
                /*if(this.isInEditMode()) {
                    return;
                }*/
                // Set the values for...
                if(!message.entityID) return false;

                this.serviceAssembly = (message.name)?message.name:"";
                this.assemblyEntityId = (message.entityID)?message.entityID:"";
                this.publishToUddi = (message.localProviders == "true")?tibco.admin.Main.getServer().getDynamicProperty("@lblYes_43"):tibco.admin.Main.getServer().getDynamicProperty("@lblNo_43");
                this.description = (message.description)?message.description:"";
                this.contact = (message.contact)?message.contact:"";
                this.archivePath = (message.archiveFile)?message.archiveFile:"";
                this.state = (message.status)?message.status:"UNKNOWN";
                this.packagedBy = (message.packagedBy)?message.packagedBy:"";
                this.exposeEndpoints = (message.exposeEndpoints)?message.exposeEndpoints:"";
                this.nameSpace = (message.namespace)?message.namespace:"";
                this.envEntityID = (message.envEntityID)?message.envEntityID:"";

                this.populateLabelsAndTextBoxes(this.block);

                //show correspond buttons depend on "isEditMode";
                if(this.isEditMode) {
                    this.setButtonDisplayType(true);
                    this.setStateBtn(this.state);
                }
                else {
                    this.setButtonDisplayType(false);
                }
            };

            /**
             *  Populate labels and textboxes.
             *  @param parentBlock {jsx3.gui.Block} Block which contains the form elements.
             */
            updateSA.prototype.populateLabelsAndTextBoxes = function(parentBlock) {
                var displayArchivePath = this.archivePath.substring(this.archivePath.lastIndexOf("\\") + 1, this.archivePath.length);
                parentBlock.getDescendantOfName('lblSANameValue_43', true, false).setText(this.serviceAssembly, true)
                parentBlock.getDescendantOfName('txtSANameValue_43', true, false).setValue(this.serviceAssembly)
                parentBlock.getDescendantOfName('lblPublishValue_43', true, false).setText(this.publishToUddi, true)

                if(this.publishToUddi == tibco.admin.Main.getServer().getDynamicProperty("@lblYes_43")) {
                    parentBlock.getDescendantOfName('rdoY_43', true, false).setSelected(jsx3.gui.RadioButton.SELECTED);
                    parentBlock.getDescendantOfName('rdoN_43', true, false).setSelected(jsx3.gui.RadioButton.UNSELECTED);
                }
                else {
                    parentBlock.getDescendantOfName('rdoY_43', true, false).setSelected(jsx3.gui.RadioButton.UNSELECTED);
                    parentBlock.getDescendantOfName('rdoN_43', true, false).setSelected(jsx3.gui.RadioButton.SELECTED);
                }
                parentBlock.getDescendantOfName('lblStatusValue_43', true, false).setText(this.publishToUddi, true)

                parentBlock.getDescendantOfName('txtContactValue_43', true, false).setValue(this.contact)
                parentBlock.getDescendantOfName('lblContactValue_43', true, false).setText(this.contact, true)
                
                
		//Start of new code for (1-8V52MF) Service Assembly download.

                parentBlock.getDescendantOfName('lblSAArchivePathValue_43', true, false).setText(displayArchivePath, false);
           	    		    	
	    	if(this.assemblyEntityId) {
	            	var saDownloadURL = tibco.admin.util.ServiceHelper.getSADownloadURL(this.assemblyEntityId);
	                parentBlock.getDescendantOfName('lblSAArchivePathValue_43', true, false).setAttribute('href',saDownloadURL);
	                parentBlock.getDescendantOfName('lblSAArchivePathValue_43', true, false).repaint()

    		}


		//End of of new code for (1-8V52MF) Service Assembly download.



                parentBlock.getDescendantOfName('txtSADescriptionValue_43', true, false).setValue(this.description);
                parentBlock.getDescendantOfName('lblSADescriptionValue_43', true, false).setText(this.description, true);
                parentBlock.getDescendantOfName('txtNamespaceValue_43', true, false).setValue(this.nameSpace);
                parentBlock.getDescendantOfName('lblNamespaceValue_43', true, false).setText(this.nameSpace, true);

                parentBlock.getDescendantOfName('lblStatusValue_43', true, false).setText(this.stateCode[this.state]?this.stateCode[this.state]:this.stateCode["UNKNOWN"], true);

                this.block.getDescendantOfName("selExposeValue_43").setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@selexposeEndDefault_43"));
                parentBlock.getDescendantOfName('selExposeValue_43', true, false).setValue(this.exposeEndpoints);
                parentBlock.getDescendantOfName('lblExposeValue_43', true, false).setText(this.exposeEndpoints, true);

                parentBlock.getDescendantOfName('lblOwnerValue_43', true, false).setText(this.packagedBy, true);
            };

            /**
             * Switch to Edit mode(Service Assembly General Info).
             */
            updateSA.prototype.editSAConfiguration = function() {
                this.setLabelsDisplayType(jsx3.gui.Block.DISPLAYNONE);
                this.setTextFieledDisplayType(jsx3.gui.Block.DISPLAYBLOCK);
                this.setButtonDisplayType(true);

                this.beginEditMode();
                this.block.getDescendantOfName("txtSANameValue_43").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
            };

            /**
             * enable save button(Service Assembly General Info).
             */
            updateSA.prototype.enableSave = function() {
                this.block.getDescendantOfName("btnSaveConfiguration_43").setEnabled(jsx3.gui.Form.STATEENABLED, true);
            };

            /**
             * Save Service Assembly Details.
             */
            updateSA.prototype.saveSAConfiguration = function() {
                if(this.block.getDescendantOfName('txtSANameValue_43').getValue() == "") {
                    tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@lblError_43"), tibco.admin.Main.getServer().getDynamicProperty("@lblEmpty_43"));
                    return false;
                }
                /* deperated (06/07/2007)
                   the mode will be initialized in pubsub callback func. -> updateSA.setDetails();
                updateSA.setLabelsDisplayType(jsx3.gui.Block.DISPLAYBLOCK);
                updateSA.setTextFieledDisplayType(jsx3.gui.Block.DISPLAYNONE);
                updateSA.setButtonDisplayType(false);
                */

                this.callupdateServiceAssemblyDetail();
            };

            /**
             * rollback all modifications,and switch to View Mode.
             */
            updateSA.prototype.cancelSAConfiguration = function() {
                //Set the original values
                this.populateLabelsAndTextBoxes(this.block);

                this.setLabelsDisplayType(jsx3.gui.Block.DISPLAYBLOCK);
                this.setTextFieledDisplayType(jsx3.gui.Block.DISPLAYNONE);
                this.setButtonDisplayType(false);
                this.endEditMode();
            };

            /**
             * Set display type of all labels.
             */
            updateSA.prototype.setLabelsDisplayType = function(displayType) {
                this.block.getDescendantOfName("lblSANameValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("lblExposeValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("lblContactValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("lblSADescriptionValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("lblPublishValue_43").setDisplay(displayType, true);
            };

            /**
             * Set display type of all TextFieleds.
             */
            updateSA.prototype.setTextFieledDisplayType = function(displayType) {
                this.block.getDescendantOfName("txtSANameValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("selExposeValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("txtContactValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("txtSADescriptionValue_43").setDisplay(displayType, true);
                this.block.getDescendantOfName("editPublishValue_43").setDisplay(displayType, true);
            };

            /**
             * Set display type of all Buttons.
             * @param eidtMode{Boolean} eidt Mode, true for edit mode, false for view mode;
             */
            updateSA.prototype.setButtonDisplayType = function(eidtMode) {
                var type1 = eidtMode?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE;
                var type2 = eidtMode?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK;
                this.block.getDescendantOfName("btnUploadSA_43").setDisplay(type1, true);

                if(type1 == jsx3.gui.Block.DISPLAYBLOCK) {
                    this.setStateBtn(this.state);
                }
//                else {
//                    this.block.getDescendantOfName("btnEditStatus_43").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
//                    this.block.getDescendantOfName("btnESUndeploy_43").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
//                }

                //Display/hide the save/edit cancel btns.
                this.block.getDescendantOfName("btnEditConfiguration_43").setDisplay(type2, true);
                this.block.getDescendantOfName("btnSaveConfiguration_43").setDisplay(type1, true);
                this.block.getDescendantOfName("btnCancelConfiguration_43").setDisplay(type1, true);

//                if(eidtMode) this.block.getDescendantOfName("btnSaveConfiguration_43").setEnabled(jsx3.gui.Form.STATEDISABLED, true);//disable save button.
            };

            /**
             * Set State Btn to deploy or undeploy.
             * @param state{String} Service Assembly Status: "deployed" or "undeployed";
             */
            updateSA.prototype.setStateBtn = function(state) {
//                if(state == "DEPLOYED") {
//                    this.block.getDescendantOfName("btnEditStatus_43").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
//                    this.block.getDescendantOfName("btnESUndeploy_43").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
//                }
//                else if(state == "NOTDEPLOYED") {
//                    this.block.getDescendantOfName("btnEditStatus_43").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
//                    this.block.getDescendantOfName("btnESUndeploy_43").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
//                }
//                else {
//                    this.block.getDescendantOfName("btnEditStatus_43").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
//                    this.block.getDescendantOfName("btnESUndeploy_43").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
//                }
            }

            /**
             * Deploy current Service Assembly.
             */
            updateSA.prototype.deploy = function() {
                this.calldeployServiceAssemblies();
            };

            /**
             * Undeploy current Service Assembly.
             */
            updateSA.prototype.undeploy = function() {
                this.callundeployServiceAssemblies();
            };

            /**
             * update Archive File of current Service Assembly.
             * @param obj {jsx3.gui.Button}  the "update" button object.
             */
            updateSA.prototype.update = function(obj) {
                var parent = tibco.admin.Main.getServer().getJSXByName("blkAdminDesktop_00");
                if(!parent) {
                    parent = objEvent.target.getServer().getRootBlock();
                }
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, "tibco.admin", "tibco/admin/deployment/sa/uploadSA/ui/uploadSA.xml", me, me.afterDialogBoxPopup, true);
            };

            /**
             * Will be called after "update Archive File" dialog box be loaded.
             * to set some styles for update mode, like disabling the Archive File Textfield.
             */
            updateSA.prototype.afterDialogBoxPopup = function(response) {
                if(response.status == "SUCCESS") {
                    tibco.admin.deployment.sa.uploadSA.UploadSA.setSANameBySADetail(false, this.assemblyEntityId,
                            this.envEntityID, this.block.getDescendantOfName("txtSANameValue_43").getValue());
                }
                else {
                    if(response.exception) {
                        tibco.admin.Main.logException(response.exception, response.message);
                    }
                    else {

                    }
                }
            };

            //call this method to begin the service call (eg.service.callupdateServiceAssemblyDetail();)
            updateSA.prototype.callupdateServiceAssemblyDetail = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/deployment/sa/updateSA/rules/updateServiceAssemblyRules.xml", "updateServiceAssemblyDetail");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                objService.assemblyEntityName = this.serviceAssembly;
                objService.assemblyEntityId = this.assemblyEntityId;

                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onupdateServiceAssemblyDetailSuccess);
                objService.doCall();
            };

            updateSA.prototype.onupdateServiceAssemblyDetailSuccess = function(objEvent) {

                // Set the subject for getting all assemblies in list

                this.cancelSAConfiguration();
                var message = new Object();
                message.subject = tibco.admin.deployment.sa.updateSA.EventID.SERVICEASSEMBLY_UPDATED;
                tibco.admin.PubSub.publish(message);

                this.updateSuccess = objEvent.target.success;
                if(this.updateSuccess != "true") {
                    tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@lblError_43"), tibco.admin.Main.getServer().getDynamicProperty("@lblUpdateFailed_43"));
                }
            };

            //call this method to begin the service call (eg.service.calldeployServiceAssemblies();)
            updateSA.prototype.calldeployServiceAssemblies = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/deployment/sa/updateSA/rules/deployServiceAssembliesRule.xml", "deployServiceAssemblies");

                objService.saEntityID = this.assemblyEntityId;
                objService.envEntityID = this.envEntityID;
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ondeployServiceAssembliesSuccess);
                objService.doCall();
            };

            updateSA.prototype.ondeployServiceAssembliesSuccess = function(objEvent) {
                if(objEvent.target.depSuccess.toLowerCase() == "true") {
                    var deployLabel = this.block.getDescendantOfName("lblStatusValue_43");
                    this.setStateBtn("DEPLOYED");
                    deployLabel.setText(this.stateCode["DEPLOYED"], true);

                    var message = new Object();
                    message.subject = tibco.admin.deployment.sa.updateSA.EventID.SERVICEASSEMBLY_UPDATED;
                    tibco.admin.PubSub.publish(message);
                }
                else {
                    tibco.admin.Main.alert(APP.tibco.admin.getServer().getDynamicProperty("@lblError_43"), APP.tibco.admin.getServer().getDynamicProperty("@lblDeployFailed_43") + "<br/>" + objEvent.target.errorDetail);
                }
            };

            //call this method to begin the service call (eg.service.callundeployServiceAssemblies();)
            updateSA.prototype.callundeployServiceAssemblies = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/deployment/sa/updateSA/rules/updeployServiceAssembliesRule.xml", "undeployServiceAssemblies");

                objService.saEntityID = this.assemblyEntityId;
                objService.envEntityID = this.envEntityID;
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onundeployServiceAssembliesSuccess);
                objService.doCall();
            };

            updateSA.prototype.onundeployServiceAssembliesSuccess = function(objEvent) {
                if(objEvent.target.undepSuccess.toLowerCase() == "true") {
                    var deployLabel = this.block.getDescendantOfName("lblStatusValue_43");
                    this.setStateBtn("NOTDEPLOYED");
                    deployLabel.setText(this.stateCode["NOTDEPLOYED"], true);

                    var message = new Object();
                    message.subject = tibco.admin.deployment.sa.updateSA.EventID.SERVICEASSEMBLY_UPDATED;
                    tibco.admin.PubSub.publish(message);
                }
                else {
                    tibco.admin.Main.alert(APP.tibco.admin.getServer().getDynamicProperty("@lblError_43"), APP.tibco.admin.getServer().getDynamicProperty("@lblUndeployFailed_43") + "<br/>" + objEvent.target.errorDetail);
                }
            };

            updateSA.prototype.subscribeAll = function() {
                var me = this;
                tibco.admin.PubSub.subscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, me, me.setDetails);
            };

            updateSA.prototype.unsubscribeAll = function() {
                tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, this);
            };

            /**
             * MUST appear in "onAfterDeserialize" textarea.
             */
            updateSA.prototype.componentLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.block = objEvent.obj;
                    var me = this;
                    this.block.handler = me;
                    this.subscribeAll()
                    // Mark the button as edit button so that it will be hiden if hasEdit = false
                    this.block.getDescendantOfName("btnEditConfiguration_43").setEditSubject(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED)//Alternatively do this in setDetails() to avoid a display logical bug - 2007-08-02;
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            updateSA.prototype.onDestroyPlugin = function() {
                this.unsubscribeAll();
                this.block = null;
                this.endEditMode();
            }

            updateSA.prototype.getComponentPath = function() {
                return  "tibco/admin/deployment/sa/updateSA/component.xml"
            }

            updateSA.prototype.spyGlass = function(elObject) {
                return "<div>" + elObject.getText() + "</div>";
            }
        });
